package com.woyou.utils;

import java.io.UnsupportedEncodingException;

import com.citaq.ideliver.R;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.TencentWbShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.TencentWBSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;
import com.woyou.Constant;
import com.woyou.bean.rpc.MD5Req;
import com.woyou.model.UserModel_;

import android.app.Activity;

/**
 * 分享工具类
 * 
 * @author 荣
 * 
 */
public class ShareUtils {
	private static Activity activity;
	private static ShareUtils shareUtils;
    private UMSocialService mController ;
    
    private ShareUtils() {
    	
	}

	/**
	 * 单例模式
	 * 
	 * @return
	 */
	public static ShareUtils getInstance(Activity a) {
		if (shareUtils == null) {
			shareUtils = new ShareUtils();
		}
		activity = a;
		return shareUtils;
	}
	
	/**
	 * 获取分享控制对象
	 * @return
	 */
	public UMSocialService getmController() {
		if ( mController == null ) {
			mController = UMServiceFactory.getUMSocialService(Constant.DESCRIPTOR);
			configPlatforms();
		}
		return mController;
	}

    /**
     * 分享订单
     */
    public void setShareOrder(String oId){
    	String shareTitle="我的惊喜发现！“我有外卖”食在方便！";
    	String shareContext="APP订餐，美食送到家！快来跟我一起叫外卖吧！点击下载http://www.woyouwaimai.com/d";
    	String shareUrl=createOrderShareUrl(oId);
        UMImage urlImage = new UMImage(activity, R.drawable.ic_launcher);
        //微信分享
        WeiXinShareContent weixinContent = new WeiXinShareContent();
        weixinContent.setTitle(shareTitle);
        weixinContent.setShareContent(shareContext);
        weixinContent.setTargetUrl(shareUrl);
        weixinContent.setShareMedia(urlImage);
        mController.setShareMedia(weixinContent);

        // 设置朋友圈分享的内容
        CircleShareContent circleMedia = new CircleShareContent();
        circleMedia.setTitle(shareTitle);
        circleMedia.setShareContent(shareContext);
        circleMedia.setTargetUrl(shareUrl);
        circleMedia.setShareMedia(urlImage);
        mController.setShareMedia(circleMedia);
        // 设置QQ空间分享内容
        QZoneShareContent qzone = new QZoneShareContent();
        qzone.setTitle(shareTitle);
        qzone.setShareContent(shareContext);
        qzone.setTargetUrl(shareUrl);
        qzone.setShareMedia(urlImage);
        mController.setShareMedia(qzone);
        //QQ分享
        QQShareContent qqShareContent = new QQShareContent();
        qqShareContent.setTitle(shareTitle);
        qqShareContent.setShareContent(shareContext);
        qqShareContent.setTargetUrl(shareUrl);
        qqShareContent.setShareMedia(urlImage);
        mController.setShareMedia(qqShareContent);
        //腾讯微博
        TencentWbShareContent tencent = new TencentWbShareContent();
        tencent.setTitle(shareTitle);
        tencent.setShareContent(shareContext);
        tencent.setTargetUrl(shareUrl);
        tencent.setShareMedia(urlImage);
        mController.setShareMedia(tencent);
        //新浪微博
        SinaShareContent sinaContent = new SinaShareContent();
        sinaContent.setTitle(shareTitle);
        sinaContent.setShareContent(shareContext);
        sinaContent.setTargetUrl(shareUrl);
        sinaContent.setShareMedia(urlImage);
        mController.setShareMedia(sinaContent);
    
    }
    
	/**
     * 分享app
     */
    public void setShareApp() {
    	String shareTitle="我的惊喜发现！“我有外卖”食在方便！";
    	String shareContext="APP订餐，美食送到家！快来跟我一起叫外卖吧！点击下载http://www.woyouwaimai.com/d";
    	String shareUrl=createAppShareUrl();
        UMImage urlImage = new UMImage(activity, R.drawable.ic_launcher);
        //微信分享
        WeiXinShareContent weixinContent = new WeiXinShareContent();
        weixinContent.setTitle(shareTitle);
        weixinContent.setShareContent(shareContext);
        weixinContent.setTargetUrl(shareUrl);
        weixinContent.setShareMedia(urlImage);
        mController.setShareMedia(weixinContent);

        // 设置朋友圈分享的内容
        CircleShareContent circleMedia = new CircleShareContent();
        circleMedia.setTitle(shareTitle);
        circleMedia.setShareContent(shareContext);
        circleMedia.setTargetUrl(shareUrl);
        circleMedia.setShareMedia(urlImage);
        mController.setShareMedia(circleMedia);
        // 设置QQ空间分享内容
        QZoneShareContent qzone = new QZoneShareContent();
        qzone.setTitle(shareTitle);
        qzone.setShareContent(shareContext);
        qzone.setTargetUrl(shareUrl);
        qzone.setShareMedia(urlImage);
        mController.setShareMedia(qzone);
        //QQ分享
        QQShareContent qqShareContent = new QQShareContent();
        qqShareContent.setTitle(shareTitle);
        qqShareContent.setShareContent(shareContext);
        qqShareContent.setTargetUrl(shareUrl);
        qqShareContent.setShareMedia(urlImage);
        mController.setShareMedia(qqShareContent);
        //腾讯微博
        TencentWbShareContent tencent = new TencentWbShareContent();
        tencent.setTitle(shareTitle);
        tencent.setShareContent(shareContext);
        tencent.setTargetUrl(shareUrl);
        tencent.setShareMedia(urlImage);
        mController.setShareMedia(tencent);
        //新浪微博
        SinaShareContent sinaContent = new SinaShareContent();
        sinaContent.setTitle(shareTitle);
        sinaContent.setShareContent(shareContext);
        sinaContent.setTargetUrl(shareUrl);
        sinaContent.setShareMedia(urlImage);
        mController.setShareMedia(sinaContent);
    }
    
	/**
     * 分享活动
     */
    public void setActivityContent(String url) {
    	String shareTitle="我的惊喜发现！“我有外卖”食在方便！";
    	String shareContext="APP订餐，美食送到家！快来跟我一起叫外卖吧！点击下载http://www.woyouwaimai.com/d";
    	String shareUrl=url;
        UMImage urlImage = new UMImage(activity, R.drawable.ic_launcher);
        //微信分享
        WeiXinShareContent weixinContent = new WeiXinShareContent();
        weixinContent.setTitle(shareTitle);
        weixinContent.setShareContent(shareContext);
        weixinContent.setTargetUrl(shareUrl);
        weixinContent.setShareMedia(urlImage);
        mController.setShareMedia(weixinContent);

        // 设置朋友圈分享的内容
        CircleShareContent circleMedia = new CircleShareContent();
        circleMedia.setTitle(shareTitle);
        circleMedia.setShareContent(shareContext);
        circleMedia.setTargetUrl(shareUrl);
        circleMedia.setShareMedia(urlImage);
        mController.setShareMedia(circleMedia);
        // 设置QQ空间分享内容
        QZoneShareContent qzone = new QZoneShareContent();
        qzone.setTitle(shareTitle);
        qzone.setShareContent(shareContext);
        qzone.setTargetUrl(shareUrl);
        qzone.setShareMedia(urlImage);
        mController.setShareMedia(qzone);
        //QQ分享
        QQShareContent qqShareContent = new QQShareContent();
        qqShareContent.setTitle(shareTitle);
        qqShareContent.setShareContent(shareContext);
        qqShareContent.setTargetUrl(shareUrl);
        qqShareContent.setShareMedia(urlImage);
        mController.setShareMedia(qqShareContent);
        //腾讯微博
        TencentWbShareContent tencent = new TencentWbShareContent();
        tencent.setTitle(shareTitle);
        tencent.setShareContent(shareContext);
        tencent.setTargetUrl(shareUrl);
        tencent.setShareMedia(urlImage);
        mController.setShareMedia(tencent);
        //新浪微博
        SinaShareContent sinaContent = new SinaShareContent();
        sinaContent.setTitle(shareTitle);
        sinaContent.setShareContent(shareContext);
        sinaContent.setTargetUrl(shareUrl);
        sinaContent.setShareMedia(urlImage);
        mController.setShareMedia(sinaContent);
    }
    
    /**
     * 配置分享平台参数</br>
     */
    private void configPlatforms() {
        // 添加新浪SSO授权
    	SinaSsoHandler mSinaSsoHandler = new SinaSsoHandler();
    	mController.getConfig().setSinaCallbackUrl("http://sns.whalecloud.com/sina2/callback");
        mController.getConfig().setSsoHandler(mSinaSsoHandler);
        mSinaSsoHandler.addToSocialSDK();
        // 添加腾讯微博SSO授权
        mController.getConfig().setSsoHandler(new TencentWBSsoHandler());
        // 添加QQ、QZone平台
        addQQQZonePlatform();
        // 添加微信、微信朋友圈平台
        addWXPlatform();
        //添加分享回调
        mController.postShare(activity,SHARE_MEDIA.WEIXIN, new SnsPostListener() {
                        @Override
                        public void onStart() {}
                        @Override
                        public void onComplete(SHARE_MEDIA platform, int eCode,SocializeEntity entity) {}
        });
        //关掉toast
        mController.getConfig().closeToast();
    }

    /**
     * 生成加密的订单Url
     */
    private String createOrderShareUrl(String oId){
		String url = "http://"+Constant.WEB_SERVER+"/orderShare2";
		Params params = new Params();
		params.oId=oId;
		params.pwd=UserModel_.getInstance_(activity).getUserInfo().getPwd();
		params.uId=UserModel_.getInstance_(activity).getUserInfo().getuId();
		MD5Req<Params> req = new MD5Req<Params>(params,"1");
		try {
			url = Utils.getWebUrl(url, req);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return url;
    }
    
    /**
     * 生成加密的分享APPUrl
     */
    private String createAppShareUrl(){
		String url = "http://"+Constant.WEB_SERVER+"/appShare2";
		Params params = new Params();
		params.uId=UserModel_.getInstance_(activity).getUserInfo().getuId();
		MD5Req<Params> req = new MD5Req<Params>(params,"1");
		try {
			url = Utils.getWebUrl(url, req);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return url;
    }
    
    /**
     * 生成加密的分享ActivityShareUrl
     */
    public String createActivityShareUrl(){
		String url = "http://"+Constant.WEB_SERVER+"/activityShare";
		Params params = new Params();
		params.uId=UserModel_.getInstance_(activity).getUserInfo().getuId();
		MD5Req<Params> req = new MD5Req<Params>(params);
		try {
			url = Utils.getWebUrl(url, req);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return url;
    }

    /**
     * 我的订单Url
     * @author 荣
     *
     */
	class Params{
		public String uId;
		public String pwd;
		public String oId;
	}

    /**
     * @功能描述 : 添加QQ平台支持 QQ分享的内容， 包含四种类型， 即单纯的文字、图片、音乐、视频. 参数说明 : title, summary,
     *       image url中必须至少设置一个, targetUrl必须设置,网页地址必须以"http://"开头 . title :
     *       要分享标题 summary : 要分享的文字概述 image url : 图片地址 [以上三个参数至少填写一个] targetUrl
     *       : 用户点击该分享时跳转到的目标地址 [必填] ( 若不填写则默认设置为友盟主页 )
     * @return
     */
    private void addQQQZonePlatform() {
        String appId = "1103558316";
        String appKey = "N7WpSzZBCmWivD42";
        // 添加QQ支持, 并且设置QQ分享内容的target url
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(activity,appId, appKey);
        qqSsoHandler.setTargetUrl("http://www.woyouwaimai.com/d");
        qqSsoHandler.addToSocialSDK();

        // 添加QZone平台
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(activity, appId, appKey);
        qZoneSsoHandler.addToSocialSDK();
    }
    
    /**
     * @功能描述 : 添加微信平台分享
     * @return
     */
    private void addWXPlatform() {
        // 注意：在微信授权的时候，必须传递appSecret
        // wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
        String appId = "wx8f92a1e3153a2a6e";
        String appSecret = "94cb2f585fabfaac421062e93ff13de0";
        // 添加微信平台
        UMWXHandler wxHandler = new UMWXHandler(activity, appId, appSecret);
        wxHandler.addToSocialSDK();

        // 支持微信朋友圈
        UMWXHandler wxCircleHandler = new UMWXHandler(activity, appId, appSecret);
        wxCircleHandler.setToCircle(true);
		wxCircleHandler.addToSocialSDK();
	}
    
}
